package org.csu.group4.jpetstore.controller;

import org.csu.group4.jpetstore.mybatis.entity.*;
import org.csu.group4.jpetstore.result.Result;
import org.csu.group4.jpetstore.result.ResultFactory;
import org.csu.group4.jpetstore.service.ItemService;
import org.csu.group4.jpetstore.service.OrderService;
import org.csu.group4.jpetstore.service.ShopCartService;
import org.csu.group4.jpetstore.service.SignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ShopCartController {
    @Autowired
    ShopCartService shopCartService;
    @Autowired
    SignOnService signOnService;
    @Autowired
    ItemService itemService;
    @Autowired
    OrderService orderService;

    @GetMapping("/viewCart")
    public ModelAndView getShopCart(){
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String)session.getAttribute("localUser");
        List<ShopCart> shopCarts = shopCartService.getShopCartsByUsername(username);
        double items = itemService.getPriceInCart(shopCarts);
        ModelAndView modelAndView =new ModelAndView("cart/Cart");
        modelAndView.addObject("has",shopCarts.size());
        modelAndView.addObject("shopCart",shopCarts);
        modelAndView.addObject( "money",items);
        return modelAndView;
    }

    @PostMapping("/viewCart/update")
    @ResponseBody
    public Result addShopCart(@RequestBody String itemId){
        itemId = itemId.replaceAll("[\"|=]","");
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String)session.getAttribute("localUser");
        ShopCart shopCart = new ShopCart();
        shopCart.setStatus(200);
        shopCart.setItemId(itemId);
        shopCart.setAccountId(username);
        shopCart.setQuantity(1);
        shopCartService.addNewShopCart(shopCart);
        System.out.println(itemId);
        return ResultFactory.getSuccessResult("添加成功");
    }

    @PostMapping("/viewCart/delete")
    @ResponseBody
    public Result deleteShopCart(@RequestBody String itemId) {
        itemId = itemId.replaceAll("[\"|=]", "");
        System.out.println(itemId);
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String) session.getAttribute("localUser");
        ShopCart shopCart = shopCartService.getItemByItemId(username, itemId);
        if (shopCart != null) {
            shopCartService.deleteItem(shopCart);
            return ResultFactory.getSuccessResult("成功删除");
        }
        return ResultFactory.getFailResult("异常");
    }
    @GetMapping("/viewCart/submit")
    public String SubmitShopCart(Model model) {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String) session.getAttribute("localUser");
        List<ShopCart> shopCarts = shopCartService.getShopCartsByUsername(username);
        List<Item> items = itemService.findallItem(shopCarts);
        Account account = signOnService.getAccountByUsername(username);
        //orderService.updateOrder(account,shopCarts);
        //List<Orders> orders = orderService.getAllByUsername(username);
        double money = itemService.getPriceInCart(shopCarts);
        model.addAttribute("shopCarts",items);
        model.addAttribute("money",money);
        return "/order/NewOrderForm";
    }

}
